package it.polimi.traveldream.ejb.bean;



import it.polimi.authentication.ejb.usermanagement.AddNewCity;
import it.polimi.authentication.ejb.usermanagement.AddNewEscursione;
import it.polimi.authentication.ejb.usermanagement.Escursione;
import it.polimi.authentication.ejb.usermanagement.Hotel;
import it.polimi.authentication.ejb.usermanagement.Pacchetto;
import it.polimi.authentication.ejb.usermanagement.Citta;
import it.polimi.authentication.ejb.usermanagement.PacchettoCitta;
import it.polimi.authentication.ejb.usermanagement.PacchettoPredefinito;
import it.polimi.authentication.ejb.usermanagement.dto.CittaDTO;
import it.polimi.authentication.ejb.usermanagement.dto.EscursioneDTO;
import it.polimi.authentication.ejb.usermanagement.dto.PacchettoPredefinitoDTO;
import it.polimi.authentication.ejb.usermanagement.dto.Pacchetto_cittaDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class UserBean
 */
@Stateless
public class AggiungiNuovaEscursione implements AddNewEscursione{


	@PersistenceContext
    private EntityManager em;
	
	public AggiungiNuovaEscursione(){	
	}
	
	@Override
	public boolean add(EscursioneDTO excursion)
	{
		ArrayList <Escursione> escApp = (ArrayList <Escursione>) em.createNamedQuery("Escursione.findThis").setParameter("nomeEsc", excursion.getNome()).getResultList();
		
		if (escApp.size() == 0)
		{
			//crea nuovo pacchetto
			Escursione newE = new Escursione (excursion);
			newE.setDisponibile(true);
			//inserisci 	
			em.persist(newE);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	@Override
	public void canc (ArrayList <String> escursioniDel)
	{
		for (int i=0; i< escursioniDel.size(); i++)
		{
			Query query = em.createQuery ("UPDATE Escursione e SET e.Disponibile = FALSE WHERE e.nomeEscursione = '"+ escursioniDel.get(i)+"'");
			int updateCount = query.executeUpdate();
		}
	}
}


